using UnityEngine;

namespace EntityKit.Runtime
{
    /// <summary>
    /// 双精度浮点型可绑定属性，使用阈值比较双精度浮点数是否相等
    /// </summary>
    public class DoubleProperty : BindableProperty<double>
    {
        private double mThreshold = 0.001;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="defaultValue">默认值</param>
        public DoubleProperty(double defaultValue = 0) : base(defaultValue)
        {
            WithComparer((a, b) => Mathf.Abs((float)(a - b)) < mThreshold);
        }

        /// <summary>
        /// 设置比较阈值
        /// </summary>
        /// <param name="threshold">新的阈值</param>
        /// <returns>当前实例</returns>
        public DoubleProperty WithThreshold(double threshold)
        {
            mThreshold = System.Math.Max(0, System.Math.Abs(threshold));
            WithComparer((a, b) => System.Math.Abs(a - b) < mThreshold);
            return this;
        }
    }
}